package tree.tree2.symmetry.isSame;

import tree.TreeNode;

/**
 * @ClassName IsSameTree
 * @Description TODO
 * @Author lenovo
 * @Date 2023-04-24 9:56
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class IsSameTree {

    /**
     * 100. 相同的树
     * 给你两棵二叉树的根节点 p 和 q ，编写一个函数来检验这两棵树是否相同。
     * <p>
     * 如果两个树在结构上相同，并且节点具有相同的值，则认为它们是相同的。
     */
    /**
     *
     * @param p
     * @param q
     * @return
     *         1                 1
     *     2       2         2      2
     */

    public boolean isSameTree(TreeNode p, TreeNode q) {

        if (p == null && q != null) {
            return false;
        }

        if (p != null && q == null) {
            return false;
        }

        if (p == null && q == null) {
            return true;
        }
        if (p.val != q.val) {
            return false;
        }

        boolean leftIsSame = isSameTree(p.left,q.left);
        boolean rightIsSame = isSameTree(p.right,q.right);
        return leftIsSame&&rightIsSame;
    }

    public static void main(String[] args) {
        IsSameTree isSameTree = new IsSameTree();
        TreeNode p = new TreeNode(1);
        p.left = new TreeNode(2);
        p.right = new TreeNode(3);

        TreeNode q = new TreeNode(1);
        q.left = new TreeNode(2);
        q.right = new TreeNode(3);

        System.out.println(isSameTree.isSameTree(p, q));
    }
}